<?php

/**
 *  tree for tests
 */
class Dclub_Tree
    extends Yz_Tree_Default
{
    public static function fromTree(Yz_Tree_Default $tree)
    {
        $i = new static;
        $i->rootElement = $tree->rootElement;
        $i->tree = $tree->tree;
        return $i;
    }

    public function applyWithEnterExit($callback, $callbackEnter, $callbackExit)
    {
        debug();

        if( ! is_callable( $callback ) ) {
            throw new Yz_Exception_InvalidArgument();
        }
        return $this->_applyToElementWithEnterExit(null, $callback, $callbackEnter, $callbackExit);
    }


    protected function _applyToElementWithEnterExit($el, $callback, $callbackEnter, $callbackExit)
    {
        if (null === $el) {
            $el = $this->rootElement;
        } else {
            call_user_func( $callback, $el );
        }
        foreach ($el->children() as $ch) {
            call_user_func( $callbackEnter, $el );
            $this->_applyToElementWithEnterExit($ch, $callback, $callbackEnter, $callbackExit);
            call_user_func( $callbackExit, $el );
        }
        return $this;
    }

}
